package com.yan.annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.aspectj.lang.annotation.Aspect;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.lang.reflect.Method;

@Aspect
@Component
public class ApiValidateAspect {

    private static final Logger log = LoggerFactory
            .getLogger(ApiValidateAspect.class);

    // @Before("within(@org.springframework.stereotype.Controller *) && @annotation(apiValidate)")

    // @Around("@annotation(com.yan.annotation.ApiValidate)")

    // @Before("within(@org.springframework.stereotype.Controller *) && @annotation(limit)")
    @Before("within(@org.springframework.stereotype.Controller *) && @annotation(apiValidate)")
    public void execute(JoinPoint joinPoint, ApiValidate apiValidate)
            throws Throwable {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getResponse();
        boolean flag = apiValidate.checkSignature();
        this.validation(joinPoint, flag, request, response);

    }

    public void validation(JoinPoint joinPoint, boolean flag,
                           HttpServletRequest request, HttpServletResponse response)
            throws Throwable {
        /* 获取请求参数 */
        String appId = request.getParameter("appid");
        String nonce = request.getParameter("nonce");
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        /* 获取方法名称 */
        String methodName = joinPoint.getSignature().getName();
        Method method = currentMethod(joinPoint, methodName);
        // ApiValidate apiValidate = method.getAnnotation(ApiValidate.class);

        log.info("开启验证:", flag);

       // throw new Exception();
        // /* 验证appId */
        // if (StrUtil.isBlank(appId))
        // throw new ApiException("appId Can not be empty!");
        // if (!StrUtil.isContain(appids, appId))
        // throw new ApiException("appId validate failed!");
        // /* 是否需要验证 Signature */
        // if (!log.checkSignature()) return joinPoint.proceed();
        // /* 验证timestamp */
        // if (StrUtil.isBlank(timestamp))
        // throw new ApiException("timestamp Can not be empty!");
        // if ((Long.parseLong(CheckSumUtil.getTimestamp()) -
        // Long.parseLong(timestamp)) > 220)
        // throw new ApiException("the signature has be Expired!");
        // /* 验证signature */
        // if (StrUtil.isBlank(signature))
        // throw new ApiException("signature Can not be empty!");
        // if (!CheckSumUtil.getCheckSum(appSecrt, nonce,
        // timestamp).trim().equals(signature.trim()))
        // throw new ApiException("API interface parameter validation failed!");

    }

    public Method currentMethod(JoinPoint joinPoint, String methodName) {
        Method[] methods = joinPoint.getTarget().getClass().getMethods();
        Method resultMethod = null;
        for (Method method : methods) {
            System.out.println("ApiValidateAspect: " + method.getName());
            if (method.getName().equals(methodName)) {
                resultMethod = method;
                break;
            }
        }
        return resultMethod;
    }
}
